package com.x.erp.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.x.erp.thirdparty.ZhiTongData;
import com.x.erp.thirdparty.ZhiTongInfo;
import com.x.erp.util.Constant;
import com.x.erp.util.SignatureUtils;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import com.x.framework.remote.HttpClient;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * 智通
 */
@RestController
@RequestMapping(value = "/zhiTong")
public class ZhiTongController extends BaseController{

	private Logger logger = LogManager.getLogger(this.getClass());

	@Autowired
	private HttpClient httpClient;

	/**
	 * 获取获取免登录 token
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/selectLoginToken", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel selectLoginToken(String shopId) {
		if (StringUtils.isEmpty(shopId)){
			return new ResultModel(ResultCode.RESULT_FAIL, "店铺ID不能为空");
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// 新建智通数据实体
		ZhiTongData zhiTongData = new ZhiTongData();
		zhiTongData.setApplicationID(Constant.ZHITONG_APPLICATIONID);
		zhiTongData.setThirdPartyAccount(shopId);
		// 获取私钥签名
		String sign = SignatureUtils.signOfZhiTong(JSON.toJSONString(zhiTongData), Constant.ZHITONG_PRIVATE_KEY);
		// 新建请求数据实体
		ZhiTongInfo zhiTongInfo = new ZhiTongInfo();
		zhiTongInfo.setSendTime(sdf.format(this.getDate(0)));
		zhiTongInfo.setSign(sign);
		zhiTongInfo.setData(zhiTongData);
		// 请求数据
		String result = null;
		try {
			result = httpClient.doPostJson(Constant.ZHITONG_SELECT_TOKEN_URL, JSON.toJSONString(zhiTongInfo));
		} catch (Exception e) {
			logger.error("获取车商用户信息失败！", e);
			return new ResultModel(ResultCode.RESULT_FAIL, "智通返回数据出现异常！");
		}
		if ("".equals(result)){
			return new ResultModel(ResultCode.RESULT_FAIL, "智通返回数据有误！");
		}
		String state = JSONObject.parseObject(result).getString("state");
		if (!state.equals("1")){
			try {
				logger.info("智通同步用户失败，提示码：" + JSONObject.parseObject(result).getString("msgCode") + "  ，提示信息：" + new String(JSONObject.parseObject(result).getString("msg").getBytes(),"UTF-8"));
			} catch (UnsupportedEncodingException e) {
				logger.error("智通获取失败提示转UTF-8编码异常！", e);
				return new ResultModel(ResultCode.RESULT_FAIL, "智通获取失败提示转UTF-8编码异常！");
			}
			return new ResultModel(ResultCode.RESULT_FAIL, "智通获取免登录token有误！");
		}
		String data = JSONObject.parseObject(result).getString("data");
		if (!StringUtils.isNotEmpty(data)){
			return new ResultModel(ResultCode.RESULT_FAIL, "智通获取免登录token有误！");
		}
		String token = JSONObject.parseObject(data).getString("token");
		Map<String, Object> map = new HashMap<>();
		map.put("token", token);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "智通获取获取免登录token成功！", map);
	}

}
